package com.niit.view;

import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Font;
import java.util.Vector;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;

import com.niit.dao.BookDao;
import com.niit.dao.impl.BookDaoImpl;
import com.niit.entity.Book;
import com.niit.listener.DelectListener;
import com.niit.listener.SearchListener;
import com.niit.listener.ToSaveBtnListener;
import com.niit.listener.ToUpdateListener;
import com.niit.utils.Constants;

public class MainView extends JFrame {

	private JLabel titleLabel,bookNameLabel;
	private JPanel searchPanel;
	private JTextField bookNameTextField;
	private JButton searchBtn,addBtn,deleteBtn,updateBtn;
	private JTable dataTable;
	private BookDao bookDao;
	private JScrollPane dataPanel;
	
	public MainView(Vector<Vector> bookList) {
		if(bookList==null){
			bookList = getTableData();
		}
		init();
		showView(bookList);
	}
	

	public JTextField getBookNameTextField() {
		return bookNameTextField;
	}
	

	public JTable getDataTable() {
		return dataTable;
	}


	public void setDataTable(JTable dataTable) {
		this.dataTable = dataTable;
	}


	private void showView(Vector<Vector> bookList) {
		titleLabel.setFont(new Font(Constants.FONT_STYLE,Font.ROMAN_BASELINE,22));
		titleLabel.setBounds(180, 20, 100, 30);
		this.add(titleLabel);
//		searchPanel.setPreferredSize(new Dimension(800, 100));
		
		searchPanel.add(bookNameLabel);
		searchPanel.add(bookNameTextField);
		searchPanel.add(searchBtn);
		searchPanel.add(updateBtn);
		
		addBtn.addMouseListener(new ToSaveBtnListener(this));
		searchPanel.add(addBtn);
		searchPanel.add(deleteBtn);
		searchPanel.setBounds(10, 50, 450, 50);
		this.add(searchPanel);
		
		
		Vector<String> titles = new Vector<>();
		titles.add("ID号");
		titles.add("书名");
		titles.add("作者");
		titles.add("出版社");
		titles.add("价格");
		titles.add("创建时间");
		titles.add("操作");
		dataTable = new JTable(bookList, titles);
		dataPanel = new JScrollPane(dataTable);
		dataPanel.setBounds(10, 100, 650, 280);
		this.add(dataPanel);
		
		this.setVisible(true);
	}
	
	private Vector<Vector> getTableData() {
		Vector<Vector> v = new Vector<Vector>();
		bookDao = new BookDaoImpl();
		Vector<Book> bookList = bookDao.bookList(null);
		for(Book b : bookList) {
			Vector ve = new Vector();
			ve.add(b.getId());
			ve.add(b.getBookName());
			ve.add(b.getBookAuthor());
			ve.add(b.getBookPrice());
			ve.add(b.getBookPublisher());
			ve.add(b.getCreateTime());
			ve.add("修改");
			v.add(ve);
		}
		return v;
	}
	
	private void init() {
		this.setTitle("主界面");
		this.setSize(680, 420);
		this.setLocationRelativeTo(null);
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		this.setLayout(null);
		
		titleLabel = new JLabel("书籍列表");
		searchPanel = new JPanel(new FlowLayout(FlowLayout.LEFT));
		bookNameLabel = new JLabel("书名:");
		bookNameTextField = new JTextField(10);
		searchBtn = new JButton("查询");
		searchBtn.addMouseListener(new SearchListener(this));
		addBtn = new JButton("新增");
		deleteBtn = new JButton("删除");
		deleteBtn.addMouseListener(new DelectListener(this));
		updateBtn = new JButton("修改");
		updateBtn.addMouseListener(new ToUpdateListener(this));
	}
}
